package main

import "fmt"

func main() {
    // example 1.11
    numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    sum := 0
    // It is the most standard and normal usage. Used in initial value / conditional expression / incremental expression form.
    for i := 0; i < len(numbers); i++ {
        sum += numbers[i]
    }
    println("Sum is ::", sum)

    //example 1.12
    // if you just have conditional expression, it works as a while loop which runs until condition statements is false
    sum2 := 0
    j := 0
    n := len(numbers)
    for j < n {
        sum2 += numbers[j]
        j++
    }
    fmt.Println("Sum is ::", sum2)

    //example 1.13
    //If you omit the initial value / conditional expression / increment or decrement expression , it works as an infinite
    // loop until the break is encountered.
    sum3 := 0
    k := 0
    nn := len(numbers)
    for {
        sum3 += numbers[k]
        k++
        if k >= nn {
            break
        }
    }
    fmt.Println("Sum is ::", sum3)

    // example 1.14
    // Using for statement with a range statements is used to traverse various elements of array, slice etc.
    sum4 := 0
    for _, val := range numbers {
        sum4 += val
        //fmt.Println(k)
    }
    fmt.Println("Sum is ::", sum4)

    //example 1.15
    // with array and slices, range provides indexes and values
    sum5 := 0
    for index, val := range numbers {
        sum5 += val
        fmt.Print("[", index, ",", val, "]")
    }
    fmt.Println("\nSum is ::", sum5)

    // with maps, range provide key-value pairs
    kvs := map[int]string{1: "apple", 2:"banana"}
    for k, v := range kvs {
        fmt.Println(k, " -> ", v)
    }
    //with string, ranges provide index of each Unicode characters in string and their corresponding Unicode characters.
    str := "Hello, World!"
    for index, c := range str {
        fmt.Print("[",index,",",string(c),"]")
    }
    // If index values are not needed then they can be discarded using _.
}
